package com.zhongan.config;

import com.zhongan.common.web.SpringBeanProxy;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.runtime.RuntimeConstants;
import org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;

import java.io.IOException;
import java.util.Properties;

@Configuration
public class JavaConfig {

	// PropertySourcesPlaceholderConfigurer这个bean，
	// 这个bean主要用于解决@value中使用的${…}占位符。
	// 假如你不使用${…}占位符的话，可以不使用这个bean。
	@Bean
	public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
		return new PropertySourcesPlaceholderConfigurer();
	}

	@Bean
	public SpringBeanProxy getSpringBeanProxy() {
		return new SpringBeanProxy();
	}

	@Bean
	VelocityEngine velocityEngine(){
		VelocityEngine ve = new VelocityEngine();
		Properties properties = new Properties();
		try {
			properties.load(this.getClass().getResourceAsStream("/velocity.properties"));
			ve = new VelocityEngine(properties);
			ve.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath");
			ve.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName());
			properties.setProperty("file.resource.loader.class",
					"org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
			properties.setProperty(Velocity.ENCODING_DEFAULT, "UTF-8");
			properties.setProperty(Velocity.OUTPUT_ENCODING, "UTF-8");
			// 初始化Velocity引擎，init对引擎VelocityEngine配置了一组默认的参数
			Velocity.init(properties);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return ve;
	}
}
